package com.microstation.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.microstation.entity.vo.ImgInfoVO;
import com.microstation.enums.ResultEnum;
import com.microstation.exception.ElementEmptyException;
import com.microstation.util.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.microstation.service.ImgInfoService;
import com.microstation.dao.ImgInfoDao;
import com.microstation.common.Assist;
import com.microstation.common.Result;
import com.microstation.entity.po.ImgInfo;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

/**
 * ImgInfo的服务接口的实现类
 *
 * @author
 */
@Service
public class ImgInfoServiceImpl implements ImgInfoService {
    private final Logger LOG = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private ImgInfoDao imgInfoDao;

    @Override
    public Result find(Assist assist) {
        //TODO这里可以做通过Assist做添加查询
        long results = imgInfoDao.getImgInfoRowCount(assist);
        List<ImgInfo> result = imgInfoDao.selectImgInfo(assist);
        return new Result(results, result);
    }

    @Override
    public ImgInfo findOne(Long id) {
        if (id == null) {
            return null;
        }
        ImgInfo result = imgInfoDao.selectImgInfoById(id);
        return result;
    }

    @Override
    public Integer saveNotNull(ImgInfo value) {
        if (value == null) {
            return null;
        }
        if (value.getBussId() == null || value.getUserType() == null) {
            throw new ElementEmptyException(ResultEnum.EMPTY.getInfo());
        }
        int result = imgInfoDao.insertNotNullImgInfo(value);
        return result;
    }

    @Override
    public Integer updateNotNullById(ImgInfo value) {
        if (value == null) {
            return null;
        }
        int result = imgInfoDao.updateNotNullImgInfoById(value);
        return result;
    }

    @Override
    public Integer deleteById(Long id) {
        if (id == null) {
            return null;
        }
        int result = imgInfoDao.deleteImgInfoById(id);
        return result;
    }

    @Override
    @Transactional
    public Result saveImgInfo(ImgInfoVO imgInfoVO) {
        Result result = new Result();
        String fileUrlStr = imgInfoVO.getFileUrlStr();
        if (StringUtil.isEmpty(fileUrlStr)) {
            result.setHasError(true);
            result.setResultEnum(ResultEnum.EMPTY);
            return result;
        }
        List<ImgInfo> list = new ArrayList<ImgInfo>();
        String[] fileUrlArr = fileUrlStr.split(";");
        ImgInfo saveParam = null;
        for (int i = 0; i < fileUrlArr.length; i++) {
            if (StringUtil.isNotEmpty(fileUrlArr[i])) {
                String[] fileArr = fileUrlArr[i].split(",");
                saveParam = new ImgInfo();
                BeanUtils.copyProperties(imgInfoVO, saveParam);
                saveParam.setFileName(fileArr[1]);
                saveParam.setUrl(fileArr[0]);
                saveParam.setDelFlag(1);
                list.add(saveParam);
            }
        }
        if (CollectionUtils.isEmpty(list)) {
            result.setHasError(true);
            result.setResultEnum(ResultEnum.EMPTY);
            return result;
        }
        imgInfoDao.insertImgInfoByBatch(list);
        result.setHasError(false);
        result.setResultEnum(ResultEnum.SUCCESS);
        return result;
    }

    @Override
    @Transactional
    public Result removeImgInfo(String ids) {
        Result result = new Result();
        if (StringUtil.isEmpty(ids)) {
            result.setHasError(true);
            result.setResultEnum(ResultEnum.EMPTY);
            return result;
        }
        Assist assist = new Assist();
        assist.setRequire(Assist.whereRequire(" and id in (",  ")", ids.split(",")));
        ImgInfo imgInfo = new ImgInfo();
        imgInfo.setDelFlag(0);
        imgInfoDao.updateNotNullImgInfo(imgInfo, assist);
        result.setHasError(false);
        result.setResultEnum(ResultEnum.SUCCESS);
        return result;
    }
}